﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YXChatApiService.Application.AccountServices.Dtos;
using YXChatApiService.Application.ChatRecordService.Dtos;
using YXChatApiService.Core.AccountEntity.DomainManager;
using YXChatApiService.Core.ChatRecordEntity;
using YXChatApiService.Core.ChatRecordEntity.DomainManager;

namespace YXChatApiService.Application.ChatRecordService
{
    public class ChatRecordAppService : IChatRecordAppService
    {
        private readonly IMapper _mapper;
        private readonly IChatRecordManager _chatRecordManager;
        private readonly IAccountManager _accountManager;

        public ChatRecordAppService(IMapper mapper, IChatRecordManager chatRecordManager, IAccountManager accountManager)
        {
            _mapper = mapper;
            _chatRecordManager = chatRecordManager;
            _accountManager = accountManager;
        }

        public async Task CreateChatRecord(CreateChatRecordInput input)
        {
            var entity = _mapper.Map<ChatRecord>(input);
            if (entity.CreateTime.Equals(default(DateTime)))
            {
                entity.CreateTime = DateTime.Now;
            }
            await _chatRecordManager.InsertAsync(entity);
        }

        public ChatRecordAccountDto GetChatRecord(string selfNumber, string buddyNumber)
        {
            var entitys = _chatRecordManager.GetBuddyAll(selfNumber, buddyNumber);
            var account = _accountManager.GetAccount(buddyNumber);
            return new ChatRecordAccountDto
            {
                Account = _mapper.Map<AccountDto>(account),
                ChatRecord = _mapper.Map<List<ChatRecordDto>>(entitys)
            };
        }

        public List<ChatRecordAccountDto> GetChatRecordAll(string selfNumber)
        {
            var entitys = _chatRecordManager.QueryAsNoTracking
                .Where(x => x.SNumber == selfNumber || x.RNumber == selfNumber)
                .OrderByDescending(n => n.CreateTime)
                .ToList();
            // self发送的所有好友
            var mySendBuddy = entitys.Where(n => n.SNumber == selfNumber).Select(n => n.RNumber).ToList();
            // 所有发送给我的好友
            var sendToMeBuddy = entitys.Where(n => n.RNumber == selfNumber).Select(n => n.SNumber).ToList();
            var allChatBuddy = mySendBuddy.Union(sendToMeBuddy).ToList();
            var allBuddyAccount = _accountManager.QueryAsNoTracking.Where(n => allChatBuddy.Contains(n.AccountNumber)).ToList();
            List<ChatRecordAccountDto> chatRecordAccounts = new List<ChatRecordAccountDto>();
            foreach (var item in allChatBuddy)
            {
                var chatRecord = entitys.Where(n => n.SNumber == item || n.RNumber == item).OrderByDescending(n => n.CreateTime).FirstOrDefault();
                var buddyAccount = allBuddyAccount.FirstOrDefault(x => x.AccountNumber == item);
                chatRecordAccounts.Add(new ChatRecordAccountDto
                {
                    Account = _mapper.Map<AccountDto>(buddyAccount),
                    ChatRecord = new List<ChatRecordDto>
                    {
                        _mapper.Map<ChatRecordDto>(chatRecord)
                    }
                });
            }
            return chatRecordAccounts;
        }
    }
}
